﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace StockPoint.Util
{
    public class StrUtil
    {

        public static string Base64Encode(string src, System.Text.Encoding enc)
        {
            byte[] arr = enc.GetBytes(src);
            return Convert.ToBase64String(arr);
        }

        public static string Base64Decode(string src, System.Text.Encoding enc)
        {
            byte[] arr = Convert.FromBase64String(src);
            return enc.GetString(arr);
        }

        /// <summary>
        /// 구분자 파싱.
        /// </summary>
        public static Hashtable ParseDelStep2(string output, string del1, string del2)
        {
            Hashtable retval = new Hashtable();

            string[] step1 = null;
            string[] step2 = null;

            step1 = System.Text.RegularExpressions.Regex.Split(output, del1);
            for (int stepc = 0; stepc < step1.Length; stepc++)
            {
                step2 = System.Text.RegularExpressions.Regex.Split(step1[stepc], del2);

                if (step2.Length < 2)
                    continue;

                retval.Add(step2[0].Trim(), step2[1].Trim());
            }
            return retval;
        }

        /// <summary>
        /// HTML 태그 제거.
        /// </summary>
        public static string RemoveHtmlTag(string value)
        {
            string regEx = @"\<[^\<\>]*\>";
            string tagless = Regex.Replace(value, regEx, string.Empty);
            tagless = tagless.Replace("<", string.Empty).Replace(">", string.Empty);
            return tagless;
        }

        /// <summary>
        /// HTML 문자 변환.
        /// </summary>
        public static string ReplaceHtmlToSChar(string Input)
        {
            Input = Regex.Replace(Input, "&amp;", "&");
            Input = Regex.Replace(Input, "&apos;", "'");
            Input = Regex.Replace(Input, "&#x22;", "\"");
            Input = Regex.Replace(Input, "&#x2039;", "<");
            Input = Regex.Replace(Input, "&#x203A;", ">");
            Input = Regex.Replace(Input, "&gt;", ">=");
            Input = Regex.Replace(Input, "&lt;", "<=");

            return Input;
        }

        public static int GetCountContains(string sValue, string sTarget)
        {
            int iCount = 0;
            int iPos = 0;
            int iIndex = 0;

            while (iIndex > -1)
            {
                iIndex = sValue.IndexOf(sTarget, iPos);

                if (iIndex > -1)
                {
                    iPos = iIndex + sTarget.Length;
                    iCount++;
                }
            }

            return iCount;
        }

        public static string GetTextTo(string sValue, string sEnd)
        {
            string sReturn = sValue;

            try
            {
                int iEnd = sValue.IndexOf(sEnd);
                if (iEnd > -1)
                    sReturn = sValue.Substring(0, iEnd);
            }
            catch
            {
            }

            return sReturn;
        }

        public static string GetTextFrom(string sValue, string sStart)
        {
            string sReturn = sValue;

            try
            {
                int iStart = sValue.IndexOf(sStart);
                if (iStart > -1)
                    sReturn = sValue.Substring(iStart + sStart.Length);
            }
            catch
            {
            }

            return sReturn;
        }

        public static string GetTextBetween(string sValue, string sStart, string sEnd)
        {
            string sReturn = string.Empty;

            try
            {
                int iStart = sValue.IndexOf(sStart);
                if (iStart > -1)
                {
                    int iEnd = sValue.IndexOf(sEnd, iStart + sStart.Length);

                    if (iEnd > -1)
                        sReturn = sValue.Substring(iStart + sStart.Length, iEnd - iStart - sStart.Length);
                    else
                        sReturn = sValue.Substring(iStart + sStart.Length);
                }
            }
            catch
            {
            }

            return sReturn;
        }

        public static string RemoveText(string sValue, string sTarget)
        {
            string sReturn = sValue;

            int iStart = sReturn.IndexOf(sTarget);
            if (iStart > -1)
                sReturn = sReturn.Remove(iStart, sTarget.Length);

            return sReturn;
        }

        public static string RemoveTextLast(string sValue, string sText)
        {
            string sReturn = sValue;

            if (sValue.Length > sText.Length && sValue.Substring(sValue.Length - sText.Length) == sText)
                sReturn = sValue.Substring(0, sValue.Length - sText.Length);

            return sReturn;
        }

        public static string RemoveTextBetween(string sValue, string sStart, string sEnd)
        {
            string sReturn = sValue;

            try
            {
                int iStart = sValue.IndexOf(sStart);
                if (iStart > -1)
                {
                    int iEnd = sValue.IndexOf(sEnd, iStart + sStart.Length);
                    if (iEnd < 0)
                        sReturn = sReturn.Remove(iStart);
                    else
                        sReturn = sReturn.Remove(iStart, iEnd - iStart);
                }
            }
            catch
            {
            }

            return sReturn;
        }

        public static string RemoveTextBetweenAll(string sValue, string sStart, string sEnd)
        {
            return RemoveTextBetweenAll(sValue, sStart, sEnd, true, true);
        }

        public static string RemoveTextBetweenAll(string sValue, string sStart, string sEnd, bool bIncludeStart, bool bIncludeEnd)
        {
            string sReturn = sValue;
            int iStart, iEnd, iPos;

            iPos = 0;
            try
            {
                while (sReturn.IndexOf(sStart, iPos) > -1)
                {
                    iStart = sReturn.IndexOf(sStart, iPos);
                    iEnd = sReturn.IndexOf(sEnd, iStart + sStart.Length);
                    if (iEnd < 0) break;

                    if (bIncludeStart)
                    {
                        if (bIncludeEnd)
                            sReturn = sReturn.Remove(iStart, iEnd - iStart + sEnd.Length);
                        else
                        {
                            sReturn = sReturn.Remove(iStart, iEnd - iStart);
                        }
                    }
                    else
                    {
                        if (bIncludeEnd)
                        {
                            sReturn = sReturn.Remove(iStart + sStart.Length, iEnd - iStart + sEnd.Length - sStart.Length);
                            iPos = iStart + sStart.Length;
                        }
                        else
                        {
                            sReturn = sReturn.Remove(iStart + sStart.Length, iEnd - iStart - sStart.Length);
                            iPos = sReturn.IndexOf(sEnd, iStart);
                        }
                    }                        
                }
            }
            catch
            {
            }

            return sReturn;
        }
    }
}
